import { Component, OnInit } from '@angular/core';
import { AlertController } from '@ionic/angular';
import { Router } from '@angular/router';
import { ActivatedRoute } from '@angular/router';
import { ApiService } from '../api/api.service';
import { ApiUrl } from '../api/ApiUrl';
import { ConsultantVO } from '../vo/ConsultantVO';
import { UserVO } from '../vo/UserVO';
import { AnswerVO } from '../vo/Answervo';
import { ToastController } from '@ionic/angular';
import { ConnectVO } from '../vo/ConnectVO';
@Component({
  selector: 'app-guwendetails',
  templateUrl: './guwendetails.page.html',
  styleUrls: ['./guwendetails.page.scss'],
})
export class GuwendetailsPage implements OnInit {
  iid: any;
  item: ConsultantVO;
  userItem: UserVO;
  iusertype: string;
  connectlist1: ConnectVO[] = [];
  connectlist2: ConnectVO[] = [];
  curl: string;
  constructor(private toastController: ToastController,
    public api: ApiService,
    public activatedRoute: ActivatedRoute,
    public alertController: AlertController,
    private router: Router) { }
  goToDetails(iid) {
    this.router.navigate(['/bankdetails', { 'iid': iid }]);
  }
  ngOnInit() {
    this.curl = 'assets/image/avatar.png';
    this.iid = this.activatedRoute.snapshot.paramMap.get('iid');
    this.onSearch(this.iid);
    this.iusertype = '0';
    if (ApiUrl.user != null) {
      this.userItem = ApiUrl.user;
      this.iusertype = this.userItem.iusertype;
    }
    console.log('iid:' + this.iid);
  }

  onSearch(iid: string) {
    this.api.getItem<ConsultantVO>(ApiUrl.getOneConsultant, { 'iid': iid }).subscribe(response => {

      if (response === null) {
        console.log('没有数据返回');
        return;
      }
      this.item = response;
      this.curl = ApiUrl.guwenUrl + this.item.cpicpath;
      console.log(this.item);
      for (let i = 0; i < this.item.vlist.length; i++) {
        this.item.vlist[i].ccpmpanypic = ApiUrl.companyUrl + this.item.vlist[i].ccpmpanypic;
        console.log(this.item.vlist[i].ccpmpanypic);
              if (this.item.vlist[i].dconnecttime !== null) {
                if (this.connectlist1.length <= 4) {
                  this.connectlist1.push(this.item.vlist[i]);
                }
              } else {
                if (this.connectlist2.length <= 4) {
                  this.connectlist2.push(this.item.vlist[i]);
                }
              }
      }
      console.log(this.connectlist1);
      console.log(this.connectlist2);
    }, () => {
      console.log('出错了');
    });
  }





  async presentAlertConfirm() {
    const alert = await this.alertController.create({
      header: '信息与服务',
      message: '<strong>是否联系该顾问？</strong>',
      buttons: [
        {
          text: '关闭',
          role: 'cancel',
          cssClass: 'secondary',
          handler: (blah) => {
            console.log('Confirm Cancel: blah');
          }
        }, {
          text: '发送',
          handler: () => {
            this.toConnect();
          }
        }
      ]
    });

    await alert.present();
  }


  toConnect() {
    this.api.getItem<AnswerVO>(ApiUrl.insertConnect, {
      'icompany': this.userItem.iorgname,
      'ccompany': this.userItem.corgname, 'icompanyuser': this.userItem.iid, 'ccompanyuser': this.userItem.cname,
      'ibank': this.item.ibank, 'cbank': this.item.cbank, 'iconsultant': this.item.iid, 'cconsultant': this.item.cname
    }).subscribe(response => {
      this.presentToast(response.msg);
    }, () => {
      console.log('出错了');
    });
  }

  async presentToast(msg) {
    const toast = await this.toastController.create({
      message: msg,
      duration: 2000
    });
    toast.present();
  }


  goToMore(itype) {
    this.router.navigate(['/servicecompanylist', { 'itype': itype, 'items': JSON.stringify(this.item.vlist)}]);
  }
}
